/**
 *
 * @author ronald
 */
package com.click.app.webapp.producers;


import com.click.app.model.Purchase;
import com.click.app.model.Supplier;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import de.laures.cewolf.DatasetProduceException;
import de.laures.cewolf.DatasetProducer;
import de.laures.cewolf.links.CategoryItemLinkGenerator;
import de.laures.cewolf.tooltips.CategoryToolTipGenerator;
import java.util.List;

public class PurchaseTotalMountReport implements DatasetProducer, CategoryToolTipGenerator, CategoryItemLinkGenerator, Serializable {

    private static final Log log = LogFactory.getLog(PurchaseTotalMountReport.class);

    // These values would normally not be hard coded but produced by
    // some kind of data source like a database or a file
    private String[] categories = {"mon", "tue", "wen", "thu", "fri", "sat", "sun"};
    private String[] seriesNames = {"Monto por pogar"};
    private Double[][] datos;
	/**
	 *  Produces some random data.
	 */
    public Object produceDataset(Map params) throws DatasetProduceException {
    	log.debug("producing data.");
        DefaultCategoryDataset dataset = new DefaultCategoryDataset(){
			/**
			 * @see java.lang.Object#finalize()
			 */
			protected void finalize() throws Throwable {
				super.finalize();
				log.debug(this +" finalized.");
			}
        };
        for (int series = 0; series < seriesNames.length; series ++) {
            for (int i = 0; i < categories.length; i++) {
                dataset.addValue(datos[series][i], seriesNames[series], categories[i]);
            }
        }
        return dataset;
    }

    public void setCategories(List<Supplier> suppliers){
        categories = new String [suppliers.size()];
        for (int i = 0; i < suppliers.size(); i++) {
            Supplier supplier = suppliers.get(i);
            categories[i] = supplier.getFirstName();
        }
    }

    public void setDatos(List<Purchase> purchases){
        datos = new Double [1][purchases.size()];
        for (int i = 0; i < purchases.size(); i++) {
            Purchase purchase = purchases.get(i);
            datos[0][i] = purchase.getRestPrice();
        }
    }
    public void addDatos(List<Purchase> purchases){
        datos = new Double [2][purchases.size()];
        for (int i = 0; i < datos.length; i++) {
            for (int j = 0; j < purchases.size(); j++) {
                Purchase purchase = purchases.get(j);
                datos[i][j] = purchase.getRestPrice();
            }
        }

    }

    /**
     * This producer's data is invalidated after 5 seconds. By this method the
     * producer can influence Cewolf's caching behaviour the way it wants to.
     */
	public boolean hasExpired(Map params, Date since) {
        log.debug(getClass().getName() + "hasExpired()");
		return (System.currentTimeMillis() - since.getTime())  > 5000;
	}

	/**
	 * Returns a unique ID for this DatasetProducer
	 */
	public String getProducerId() {
		return "PageViewCountData DatasetProducer";
	}

    /**
     * Returns a link target for a special data item.
     */
    public String generateLink(Object data, int series, Object category) {
        return seriesNames[series];
    }

	/**
	 * @see java.lang.Object#finalize()
	 */
	protected void finalize() throws Throwable {
		super.finalize();
		log.debug(this + " finalized.");
	}

	/**
	 * @see org.jfree.chart.tooltips.CategoryToolTipGenerator#generateToolTip(CategoryDataset, int, int)
	 */
	public String generateToolTip(CategoryDataset arg0, int series, int arg2) {
		return seriesNames[series];
	}

}
